const wx2my = require('../../wx2my');

// pages/device_detail/device_detail.js
const app = getApp();
Page({
  data: {
    tab: [{
      "name": "开灯",
      "index": 0,
      "select": 0,
      "type": 0
    }, {
      "name": "关灯",
      "index": 1,
      "select": 0,
      "type": 1
    }],
    endTime: '',
    startTime: '',
    active: 'active',
    switch1Checked: false,
    switch2Checked: false,
    chooseDoor: true,
    openSus: true,
    repSus: true,
    powerFail: true,
    handFail: true,
    handBackagain: 'handBackagain',
    powerBackagain: 'powerBackagain',
    select1: 0,
    select2: 0,
    stopPull: ''
  },
  onLoad: function (e) {
    this.setData({
      id: e.id,
      macno: e.macno
    });
  },
  onReady: function () {},
  onShow: function () {
    this.get_msg();
    this.get_detaiStaus();
  },

  get_msg() {
    let that = this;
    app.wxRequest('macDetail', {
      'token': wx2my.getStorageSync('token'),
      'device_id': that.data.id
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          msg: res.data.data
        });
      }
    });
  },

  get_detaiStaus() {
    let that = this;
    app.wxRequest('get_mac_info', {
      'macno': that.data.macno
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        if (res.data.data) {
          if (res.data.data.lighting_status == 0) {
            that.data.tab.forEach((item, index) => {
              item.index = item.index;

              if (item.index == 0) {
                item.select = 1;
              }

              that.setData({
                tab: that.data.tab
              });
            });
          } else if (res.data.data.lighting_status == 1) {
            console.log(res.data.code == 1);
            that.data.tab.forEach((item, index) => {
              item.index = item.index;

              if (item.index == 1) {
                item.select = 1;
              }

              that.setData({
                tab: that.data.tab
              });
            });
          }

          if (res.data.data.tasks_status == 0) {
            that.setData({
              switch1Checked: true
            });
            that.setData({
              active: '',
              action: 1
            });
            that.data.tab.forEach((item, index) => {
              item.select = item.select;

              if (item.select == 1) {
                item.select = 2;
              }

              that.setData({
                tab: that.data.tab
              });
            });
          }

          if (res.data.data.vol_check == 0) {
            that.setData({
              switch2Checked: true
            });
          } else {
            that.setData({
              switch2Checked: false
            });
          }

          if (res.data.data.beginTime) {
            that.setData({
              startTime: res.data.data.beginTime,
              endTime: res.data.data.endTime
            });
          }
        }
      }
    });
  },

  /*开关灯切换 */
  active(e) {
    if (e.currentTarget.dataset.select == 1) {
      return false;
    }

    var that = this;
    let tab = that.data.tab;
    tab.forEach((item, index) => {
      if (index == e.currentTarget.dataset.index) {
        item.select = 1;
      } else {
        item.select = 0;
      }
    });
    let type = e.currentTarget.dataset.type;
    that.setData({
      tab: tab,
      type: type
    });
    app.wxRequest('light_set', {
      'macno': that.data.macno,
      'type': 0,
      'action': that.data.type
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  //定时开启
  switch1Change(e) {
    let that = this;
    let tab = that.data.tab;
    console.log(that.data.startTime);

    if (that.data.startTime == '') {
      app.toast("请选择开启时间");
      that.setData({
        switch1Checked: false
      });
    } else if (that.data.endTime == '') {
      app.toast("请选择结束时间");
      that.setData({
        switch1Checked: false
      });
    } else if (e.detail.value == true) {
      that.setData({
        active: '',
        action: 1
      });
      tab.forEach((item, index) => {
        item.select = item.select;

        if (item.select == 1) {
          item.select = 2;
        }

        that.setData({
          tab: tab
        });
      });
      that.get_openTime();
    } else {
      tab.forEach((item, index) => {
        item.select = item.select;

        if (item.select == 2) {
          item.select = 1;
        }

        that.setData({
          tab: tab
        });
      });
      that.setData({
        active: 'active',
        action: 0
      });
      that.get_openTime();
    }
  },

  get_openTime() {
    let that = this;
    app.wxRequest('timing_light', {
      'macno': that.data.macno,
      'type': 0,
      'action': that.data.action,
      'beginTime': that.data.startTime,
      'endTime': that.data.endTime
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
      } else {
        if (that.data.switch1Checked == false) {
          that.setData({
            switch1Checked: false
          });
          that.data.tab.forEach((item, index) => {
            item.select = item.select;

            if (item.select == 2) {
              item.select = 1;
            }

            that.setData({
              tab: that.data.tab
            });
          });
          that.setData({
            active: 'active',
            action: 0
          });
          app.toast(res.data.msg);
        } else {
          that.setData({
            switch1Checked: true
          });
          that.data.tab.forEach((item, index) => {
            item.select = item.select;

            if (item.select == 1) {
              item.select = 2;
            }

            that.setData({
              tab: that.data.tab
            });
          });
          that.setData({
            active: '',
            action: 1
          });
          app.toast(res.data.msg);
        }
      }
    });
  },

  bindTimeChange: function (e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      startTime: e.detail.value
    });
  },

  endTimeChange(e) {
    console.log(e.detail.value);
    let that = this;
    that.setData({
      endTime: e.detail.value
    });
  },

  //背景音乐
  switch2Change(e) {
    let that = this;
    console.log(e.detail.value);

    if (e.detail.value == true) {
      that.setData({
        openLight: 0
      });
      that.get_lightStatus();
    } else {
      that.setData({
        openLight: 1
      });
      that.get_lightStatus();
    }
  },

  get_lightStatus() {
    let that = this;
    app.wxRequest('sound_set', {
      'macno': that.data.macno,
      'type': that.data.openLight
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        app.toast(res.data.msg);
      } else {
        if (that.data.switch2Checked == false) {
          that.setData({
            switch2Checked: false
          });
        } else {
          that.setData({
            switch2Checked: true
          });
        }

        app.toast(res.data.msg);
      }
    });
  },

  //扫码检查
  tocheck() {
    let that = this;
    wx2my.scanCode({
      success: res => {
        console.log(res);
        var res = res.result;

        if (res.indexOf('macno') < 0) {
          wx2my.showToast({
            icon: 'none',
            title: '二维码无效'
          });
        } else {
          let macno = app.fm.getUrlParam('macno', res);
          wx2my.setStorageSync('macno', macno);
          wx2my.navigateTo({
            url: '/pages/door_check/door_check?macno=' + macno
          });
        }
      }
    });
  },

  //扫码补货
  replenish(e) {
    let that = this;
    wx2my.scanCode({
      success: res => {
        console.log(res);
        var res = res.result;

        if (res.indexOf('macno') < 0) {
          wx2my.showToast({
            icon: 'none',
            title: '二维码无效'
          });
        } else {
          let macno = app.fm.getUrlParam('macno', res);
          wx2my.setStorageSync('macno', macno);
          that.setData({
            macno: macno,
            chooseDoor: false
          });
        }
      }
    });
  },

  //是否开门成功
  get_openDoor() {
    let that = this;
    app.wxRequest('macMending', {
      'token': wx2my.getStorageSync('token'),
      'macno': that.data.macno,
      'caseno': that.data.box
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let yun_id = res.data.data.yun_id;
        that.setData({
          yun_id: yun_id,
          openSus: false,
          chooseDoor: true
        });
        setTimeout(() => {
          that.get_pull();
        }, 2000);
      } else {
        that.setData({
          select1: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  //开门轮询
  get_pull() {
    let that = this;
    that.setData({
      openSus: true
    });
    app.wxRequest('Mending_Polling', {
      'yun_id': that.data.yun_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 100) {
        that.setData({
          repSus: false
        });
        wx2my.hideLoading();
      } else if (res.data.code == 22) {
        wx2my.showLoading({
          title: '补货中',
          mask: true
        });
        that.data.stopPull = setTimeout(function () {
          that.get_pull();
        }, 2000);
      } else if (res.data.code == -50) {
        that.setData({
          handFail: false
        });
        wx2my.hideLoading();
      } else {
        that.setData({
          powerFail: false
        });
        wx2my.hideLoading();
      }
    });
  },

  del() {
    this.setData({
      chooseDoor: true
    });
  },

  one() {
    let that = this;
    that.setData({
      chooseDoor: false,
      select1: 1,
      box: 1
    });
    that.get_openDoor();
  },

  two() {
    let that = this;
    that.setData({
      chooseDoor: false,
      select2: 2,
      box: 2
    });
    that.get_openDoor();
  },

  //重新开门
  get_againOpen() {
    let that = this;
    app.wxRequest('reopen_door', {
      'token': wx2my.getStorageSync('token'),
      'yun_id': that.data.yun_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 100) {
        setTimeout(() => {
          that.get_pull();
        }, 2000);
      } else {
        that.setData({
          select1: 0,
          select2: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  powerBackagain() {
    let that = this;
    that.setData({
      powerFail: true
    });
    that.get_againOpen();
  },

  handBackagain() {
    let that = this;
    that.setData({
      handFail: true
    });
    that.get_againOpen();
  },

  toSus() {
    this.setData({
      repSus: true
    });
    this.get_msg(); // wx.navigateBack()
  },

  onHide: function () {},
  onUnload: function () {
    let that = this;
    clearTimeout(that.data.stopPull);
    that.setData({
      stopPull: ''
    });
  },
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});